//
//  JourneyDetailTableViewController.m
//  WildDonkey
//
//  Created by 孙俊 on 15/3/30.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "JourneyDetailTableViewController.h"

@interface JourneyDetailTableViewController ()

@property (nonatomic,retain) NSMutableData *data;
@property (nonatomic,retain) NSMutableArray *array;

@property (nonatomic,retain) JourneyDetailData *model;

@end


@implementation JourneyDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	//[self.view setBackgroundColor:[UIColor redColor]];
	
	//self.navigationItem.title = self.model.title;
	self.navigationItem.title = self.yid;
	
	NSString *urlstr = [NSString stringWithFormat:@"http://mobileapp.roadqu.com/api/mobile/qunawan/poi/youji?token=5444d64083242&yid=%@",self.yid];
	
	NSURL *url = [NSURL URLWithString:urlstr];
	NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:20];

	[[NSURLConnection alloc] initWithRequest:request delegate:self];

	[request release];
	//request = nil;


    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - ConnectionDataDelegate
//返回请求
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
	self.data = [NSMutableData data];
}
//多次返回数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
	[self.data appendData:data];
}
//数据下载完毕之后
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
	NSDictionary *dic1 = [NSJSONSerialization JSONObjectWithData:_data options:(NSJSONReadingMutableLeaves) error:nil];
	
	NSDictionary *dic2 = [dic1 valueForKey:@"data"];
	
	self.model = [[JourneyDetailData alloc] init];
	
	[self.model setValuesForKeysWithDictionary:dic2];

	NSLog(@"%@",self.model.content);//数据请求成功
	NSLog(@"model request is OK");

	
}
//网络请求出现错误
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
	if (error) {
		NSLog(@"[JourneyDetailTableViewController]网络请求数据 %@",error);
	}else{
		NSLog(@"[JourneyDetailTableViewController]网络请求数据 OK");

	}
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//	if (0 == indexPath.section) {
//		return [UIScreen mainScreen].bounds.size.height / 3 ;
//	}
		return 1000;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *reuseIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
	if (nil == cell) {
		cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:reuseIdentifier];
	}
	
	
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
